package com.demo.SocketServer.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.demo.SocketServer.ISocketServer;

public class BIOSocketServer implements ISocketServer {

	private ServerSocket serverSocket;

	// 记录客户端的数量
	public static int clinetCount = 0;

	// 在线客户端数量
	public static int onlineClinet = 0;

	boolean isRun = true;

	@Override
	public void start() throws IOException {
		serverSocket = new ServerSocket(8888);
		Socket socket = null;
		System.out.println("***服务器即将启动，等待客户端的连接***");
		// 循环监听等待客户端的连接
		while (isRun) {
			// 调用accept()方法开始监听，等待客户端的连接
			socket = serverSocket.accept();
			// 创建一个新的线程
			ServerThread serverThread = new ServerThread(socket);
			// 启动线程
			serverThread.start();
			clinetCount++;// 统计客户端的数量
		}
	}

	@Override
	public void close() {
		isRun = false;
		try {
			if (serverSocket != null) {
				serverSocket.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
